package com.zerocarbon.framework.core.exception;

import com.zerocarbon.framework.common.enums.BaseExceptionCode;

/**
 * 业务异常
 * @author wyf
 * @date 2020年3月5日
 */
public class BusinessException extends BaseUncheckedException {
	
    private static final long serialVersionUID = 6610083281801529147L;

    public BusinessException(String message) {
        super(BaseExceptionCode.SYSTEM_ERROR.getCode(), message);
    }

    public BusinessException(int code, String message) {
        super(code, message);
    }

    public BusinessException(int code, String message, Object... args) {
        super(code, message, args);
    }
    
    /**
     * 实例化异常
     * @param code    自定义异常编码
     * @param message 自定义异常消息
     * @param args    已定义异常参数
     * @return
     */
    public static BusinessException wrap(int code, String message, Object... args) {
        return new BusinessException(code, message, args);
    }

    /**
     * 系统异常
     * @param message
     * @param args
     * @return
     */
    public static BusinessException wrap(String message, Object... args) {
        return new BusinessException(BaseExceptionCode.SYSTEM_ERROR.getCode(), message, args);
    }

    /**
     * 参数验证异常
     * @param message
     * @param args
     * @return
     */
    public static BusinessException validFail(String message, Object... args) {
        return new BusinessException(BaseExceptionCode.VALID_PARAM_EX.getCode(), message, args);
    }

    public static BusinessException wrap(BaseExceptionCode ex) {
        return new BusinessException(ex.getCode(), ex.getMessage());
    }

    @Override
    public String toString() {
        return "BusinessException [message=" + message + ", code=" + code + "]";
    }
}
